import 'reflect-metadata'
import { Component } from "../annotation/ioc";
import { Transactional } from "../annotation/aop";
import { autoBind, ContainerProvider } from "../container";

interface TaDao {
  insert(text: string): number;

  update(id: number, text: string): number;

  select(id: number): string;

  delete(id: number): number;
}

declare let taDao: TaDao;

@Transactional({readOnly: true})
@Component()
export class A {

  name = "testAAA";

  select(a: string, b: string) {
    console.log("select | this.name=", this.name, " | a=", a, " | b=", b);
    this.update(a, b);
    this.delete(a, b);
  }

  @Transactional()
  update(a: string, b: string) {
    console.log("update | this.name=", this.name, " | a=", a, " | b=", b);
  }

  @Transactional()
  delete(a: string, b: string) {
    console.log("delete | this.name=", this.name, " | a=", a, " | b=", b);
  }

  @Transactional()
  insert(a: string, b: string) {
    console.log("insert | this.name=", this.name, " | a=", a, " | b=", b);
  }

  @Transactional()
  test() {
    const id = taDao.insert("123");
    console.log("id = ", id);
  }
}

// 初始化IOC容器
ContainerProvider.init();
ContainerProvider.provide().load(autoBind());

const a = ContainerProvider.provide().get<A>(A);
// console.log("--------------------------------------------------------------------------------->>>");
// a.select("aaa", "bbb");
// a.update("aaa", "bbb");
// a.delete("aaa", "bbb");
// a.insert("aaa", "bbb");
// console.log("<<<---------------------------------------------------------------------------------");

// console.log("--------------------------------------------------------------------------------->>>");
// a.select("aaa", "bbb");
// a.update("aaa", "bbb");
// a.delete("aaa", "bbb");
// a.insert("aaa", "bbb");
// console.log("<<<---------------------------------------------------------------------------------");

const test = function () {
  console.log("--------------------------------------------------------------------------------->>>");
  // a.select("aaa", "bbb");
  // a.update("aaa", "bbb");
  // a.delete("aaa", "bbb");
  // a.insert("aaa", "bbb");
  a.test();
  console.log("<<<---------------------------------------------------------------------------------");
}

export {
  a,
  test,
}
